<%@Page Language="VB"%>

<%@Import Namespace="System.Data" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<%@ Register TagPrefix="wrox" TagName="getdataset" Src="..\global\get-dataset-control.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Extracting Nested XML from a DataSet</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Extracting Nested XML from a DataSet</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>

<%'-- insert the control that creates the DataSet --%>
<wrox:getdataset id="ctlDataSet" runat="server"/>

<b>DataSet.Tables Collection</b>
<asp:datagrid id="dgrTables" runat="server" /><br />
<b>DataSet.Relations Collection</b>
<asp:datagrid id="dgrRelsNormal" runat="server" />
<div id="outResultNormal" runat="server" /><p />

<b>DataSet.Relations Collection</b>
<asp:datagrid id="dgrRelsNested" runat="server" />
<div id="outResultNested" runat="server" /><p />

<script language="vb" runat="server">

Sub Page_Load()

   'get connection string from ..\global\connect-strings.ascx user control
   Dim strConnect As String = ctlConnectStrings.OLEDBConnectionString

   'create a variable to hold an instance of a DataSet object
   Dim objDataSet As DataSet

   'get dataset from get-dataset-control.ascx user control
   objDataSet = ctlDataSet.BooksDataSet(strConnect, "ISBN LIKE '18610033%'")

   If IsNothing(objDataSet) Then Exit Sub

   'now we're ready to display the contents of the DataSet object
   'bind the collection of Tables to the first DataGrid on the page
   dgrTables.DataSource = objDataSet.Tables
   dgrTables.DataBind()

   'bind the collection of Relations to the second DataGrid on the page
   dgrRelsNormal.DataSource = objDataSet.Relations
   dgrRelsNormal.DataBind()

   'now we can save the DataSet contents to an XML disk file
   Dim strResult As String

   Try

      'use the path to the current virtual application
      Dim strVirtualPath As String = Request.ApplicationPath & "Normal-XML-from-DataSet.xml"

      'write the data and schema from the DataSet to an XML document on disk
      'must use the Physical path to the file not the Virtual path
      objDataSet.WriteXML(Request.MapPath(strVirtualPath))
      outResultNormal.innerHTML = "Written file: <b><a href=" & Chr(34) & strVirtualPath _
                & Chr(34) & ">" & strVirtualPath & "</a></b>"

   Catch objError As Exception

      'display error details
      outResultNormal.innerHTML = "<b>* Error while writing disk file</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

   'now set the Nested property of the two relation objects
   objDataSet.Relations("BookAuthors").Nested = True
   objDataSet.Relations("BookPrices").Nested = True

   'bind the collection of Relations to the third DataGrid on the page
   dgrRelsNested.DataSource = objDataSet.Relations
   dgrRelsNested.DataBind()

   'now save the DataSet contents to another XML disk file

   Try

      'use the path to the current virtual application
      Dim strVirtualPath As String = Request.ApplicationPath & "Nested-XML-from-DataSet.xml"

      'write the data and schema from the DataSet to an XML document on disk
      'must use the Physical path to the file not the Virtual path
      objDataSet.WriteXML(Request.MapPath(strVirtualPath))
      outResultNested.innerHTML = "Written file: <b><a href=" & Chr(34) & strVirtualPath _
                & Chr(34) & ">" & strVirtualPath & "</a></b>"

   Catch objError As Exception

      'display error details
      outResultNested.innerHTML = "<b>* Error while writing disk file</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

End Sub
</script>


<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
